﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;

namespace ISmart
{
    /// <summary>
    /// 解析XML文档
    /// </summary>
    public class AnalysisXml
    {
        readonly string NameSpace;
        XmlNamespaceManager xnm;
        XmlDocument xmlDoc;

        public AnalysisXml()
        {
            NameSpace = "weetry.com";
        }

        public AnalysisXml(string nameSpace)
        {
            NameSpace = nameSpace;
        }

        /// <summary>
        /// 通过HTTP请求获取XML文档
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="parameters">参数</param>
        /// <param name="method">提交方式</param>
        /// <returns>XmlDocument文档</returns>
        public XmlDocument GetXmlDoc(string url, KeyValue[] parameters, HttpMethod method = HttpMethod.POST)
        {
            xmlDoc = new XmlDocument();
            StringBuilder urlParameter = new StringBuilder();
            byte[] postDate;
            HttpWebRequest request;

            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
            }
            catch
            {
                return null;
            }

            foreach (KeyValue kv in parameters)
            {
                urlParameter.AppendFormat("{0}={1}&", kv.Key, kv.Value);
            }

            if (urlParameter.Length > 0)
            {
                urlParameter.Remove(urlParameter.Length - 1, 1);
            }

            postDate = new UTF8Encoding().GetBytes(urlParameter.ToString());
            request.Method = method.ToString();
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postDate.Length;

            using (Stream streamParameter = request.GetRequestStream())
            {
                streamParameter.Write(postDate, 0, postDate.Length);
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                xmlDoc.LoadXml(reader.ReadToEnd());
                reader.Close();
            }

            if (xmlDoc != null)
            {
                xnm = new XmlNamespaceManager(xmlDoc.NameTable);
            }
            else
            {
                return null;
            }

            xnm.AddNamespace("Root", NameSpace);

            return xmlDoc;
        }

        /// <summary>
        /// 获取XML中的节点列表
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <returns>XmlNodeList节点列表</returns>
        public XmlNodeList GetXmlNodeList(string nodeName)
        {
            XmlNodeList xnl = xmlDoc.SelectNodes(String.Format("//Root:{0}", nodeName), xnm);

            return xnl;
        }

        /// <summary>
        /// 获取节点中字段的值
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="field">字段名</param>
        /// <returns>相关字段的值</returns>
        public string GetXmlNodeText(XmlNode node, string field)
        {
            foreach (XmlNode xn in node.ChildNodes)
            {
                if (xn.Name == field)
                {
                    return xn.InnerText;
                }
            }

            return "";
        }
    }
}
